$(function() {
    //获取id
    var id = tools.queryString().id || 1;
    //页面加载时默认样式
    $($(".productMsgOrComments")[0]).addClass("showMsgOrComments")
    $($(".allCommentList")).addClass("showcommentList");
    //获取商品详情
    $.ajax({
        type: "get",
        url:domain.online + "/api/product",
        // url: domain.test + "/getproductdetail",
        data: {id: id},
        success: function(data) {
            // if(data.data.rtnCode === 200) {
            if(data.rtnCode === 200) {
                // var productDetail = data.data.data;
                var productDetail = data.data;
                //商品图片轮播图
                $(".swiper-container").html(template("productAutoImg", { pics: productDetail.pics }));
                var mySwiper = new Swiper('.swiper-container', {
                    autoplay: 3000,
                    pagination: ".swiper-pagination",
                    loop: true,
                    autoplayDisableOnInteraction: false
                });
                //商品信息
                document.title = productDetail.productName;
                $(".product_Name").html(template("productMsgAndPrice", { msg: productDetail }));
                //新欢商品
                $('.like').html(template("peopleLikeProduct", { love: productDetail.love }));
                //商品详情
                $(".productMsg").html(template("msgContent", { details: productDetail.details }));
                $("img.lazy").lazyload({
                    palceholder:"../../img/loading.gif",
                    effect: "fadeIn", 
                    threshold: 50,  
                    failurelimit : 10 
                });
            }
        }
    });
    //获取商品评论数
    $.ajax({
        type: "get",
        // url: domain.test + "/getproductcommentsnum",
        url:domain.online + "/api/product/review/number",
        data:{id:id},
        success:function(data) {
            // if(data.data.rtnCode === 200) {
            if(data.rtnCode === 200) {
                // var commentsNum = data.data.data;
                var commentsNum = data.data;
                $(".btn").html(template("btns",{commentsNum:commentsNum.hao+commentsNum.zhong+commentsNum.cha}));
                $($(".msgAndcommentsBtn")[0]).addClass("btnActive");
                $(".commentClassify").html(template("commentClassifyBtn",{allCommentNum:commentsNum}));
                $($(".commentItem")[0]).addClass("activeComments");
            }
        }
    });
    //获取所有商品评论
    $.ajax({
        type: "get",
        // url: domain.test + "/getproductallcomments",
        url: domain.online + "/api/product/review/list",
        data:{id:id},
        success:function(data) {
            // if(data.data.rtnCode === 200) {
            if(data.rtnCode === 200) {
                // var comments = data.data.data;
                var comments = data.data;
                $(".allCommentList").html(template("allCommentsTpl",{commenList:comments.list}));
                getGoodComments(id);
            }
        }
    });
    //获取商品好评列表
    function getGoodComments(id){
        $.ajax({
        type: "get",
        // url: domain.test + "/getproductgoodcomments",
        url: domain.online + "/api/product/review/list",
        data:{id:id,score:1,pageNum:1},
        success:function(data) {
            // if(data.data.rtnCode === 200) {
            if(data.rtnCode === 200) {
                // var comments = data.data.data;
                var comments = data.data;
                $(".goodCommentList").html(template("goodCommentsTpl",{commenList:comments.list}));
                getNeutralComments(id);
            }
        }
    });
    }
    //获取商品中评列表
    function getNeutralComments(id) {
        $.ajax({
        type: "get",
        // url: domain.test + "/getproductneutralcomments",
        url: domain.online + "/api/product/review/list",
        data:{id:id,score:0,pageNum:1},
        success:function(data) {
            // if(data.data.rtnCode === 200) {
            if(data.rtnCode === 200) {
                // var comments = data.data.data;
                var comments = data.data;
                $(".neutralCommentList").html(template("neutralCommentsTpl",{commenList:comments.list}));
                getBadComments(id);
            }
        }
    });
    }
    //获取商品差评列表
    function getBadComments(id) {
        $.ajax({
        type: "get",
        // url: domain.test + "/getproductbadcomments",
        url: domain.online + "/api/product/review/list",
        data:{id:id,score:-1,pageNum:1},
        success:function(data) {
            // if(data.data.rtnCode === 200) {
            if(data.rtnCode === 200) {
                // var comments = data.data.data;
                var comments = data.data;
                $(".badCommentList").html(template("badCommentsTpl",{commenList:comments.list}));
            }
        }
    });
    }
    //商品详情和评论的切换
    $(".btn").on("click",".msgAndcommentsBtn",function() {
        $(".msgAndcommentsBtn").removeClass("btnActive");
        $(this).addClass("btnActive");
        var index = $(this).attr("index");
        $(".productMsgOrComments").removeClass("showMsgOrComments");
        $($(".productMsgOrComments")[index - 1]).addClass("showMsgOrComments");
    });
    //商品评论的切换
    $(".commentClassify").on("click",".commentItem",function() {
        $(".commentItem").removeClass("activeComments");
        $(this).addClass("activeComments");
        var index = $(this).attr("index");
        $(".commentList").removeClass("showcommentList");
        $($(".commentList")[index - 1]).addClass("showcommentList");
    });
    //客服弹出层
    $(".kefu").on("click",function() {
        var width = window.screen.availWidth;
        var height = window.screen.availHeight;
        $(".kefuMarsk").css({
            "width":width,
            "height":height,
            "display":"block"
        });
    });
    //隐藏客服弹出层
    $(".cancle").on("click",function() {
        $(".kefuMarsk").css("display","none");
    });
    //复制客服微信
    var clipboardOne = new Clipboard(document.querySelector(".copyBtnOne"));
    var clipboardTwo = new Clipboard(document.querySelector(".copyBtnTwo"));
    //加入购物车
    $(".addCar").on("click",function() {
        marskAnimate("加入购物车");
    });
    $(".buy").on("click",function() {
        marskAnimate("立即购买");
    });
    function marskAnimate(str) {
        var width = window.screen.availWidth;
        var height = window.screen.availHeight;
        $(".footerBtn").html(str);
        $(".carAndBuyMarsk").css({
            "width":width,
            "height":height,
            "background-color":"rgba(0,0,0,.6)",
            "display":"block"
        });
        $(".cbmBox").animate({
            "bottom":0
        },600);
    }
    //关闭弹出层
    $(".closeBtn").on("click",function(){
        cbmBoxAnimate();
    });
    $(".carAndBuyMarsk").on("click",function() {
        cbmBoxAnimate();
    });
    function cbmBoxAnimate() {
        $(".carAndBuyMarsk").css("background-color","rgba(0,0,0,0)");
        $(".footerBtn").html()
        $(".cbmBox").animate({
            "bottom":-830
        },500,function() {
            $(".carAndBuyMarsk").css("display","none");
            $(".addOrCut .num").html("1");
        }); 
    }
    $(".cbmBox").on("click",function(event){
        event.stopPropagation();
    });
    //商品加减
    $(".add").on("click",function() {
        var num = parseFloat($(".addOrCut .num").html());
        num++;
        $(".addOrCut .num").html(num);
    });
    $(".cut").on("click",function() {
        var num = parseFloat($(".addOrCut .num").html());
        if(num===1) {
            return;
        }else {
            num--;
            $(".addOrCut .num").html(num);
        }
    });
});